#define STB_IMAGE_IMPLEMENTATION
#include"stb_image.h"
#include "image.h"
#include <iostream>

Image::Image(const uint32_t& width, const uint32_t& height, RGBA* data) {
	mWidth = width;
	mHeight = height;
	if (data) {
		mData = new RGBA[mWidth * mHeight];
		memcpy(mData, data, sizeof(RGBA) * mWidth * mHeight);
	}
}

Image::~Image() {
	if (mData != nullptr) {
		delete[] mData;
	}
}

Image* Image::createImage(const std::string& path) {
	int picType = 0;
	int width{ 0 }, height{ 0 };
   
	//y轴翻转    ubuntu上不需要，ubuntu坐标原点为左上角，windows坐标原点为左下角才需要
	//stbi_set_flip_vertically_on_load(true);

    //加载图片 读取图片宽高像素点，图片类型
	unsigned char* bits = stbi_load(path.c_str(), &width, &height, &picType, STBI_rgb_alpha);
	Image* image = new Image(width, height, (RGBA*)bits);
	stbi_image_free(bits);

	return image;

}

void Image::destroyImage(Image* image) {
	if (image) {
		delete image;
	}
}
